package com.zis.system.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.RoleResDao;
import com.zis.system.inter.IRoleResInter;
import com.zis.system.model.RoleRes;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/4/11
 */
@Service
@Transactional("txManager")
public class RoleResInterImpl implements IRoleResInter {

    @Autowired
    private RoleResDao roleResDao;

    @Override
    public ResponseEntity insert(RoleRes model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity update(RoleRes model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        return null;
    }

    @Override
    public PageQuery<RoleRes> findByPage(SearchParams params) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        return null;
    }

    /**
     * 角色资源保存
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity roleResSave(JSONObject params) throws Exception {
        if (!params.containsKey("roleId") || params.getString("roleId").equals("")) {
            return ResponseEntity.failure("角色信息为空");
        }
        if (!params.containsKey("roleRes") || params.getString("roleRes").equals("")) {
            return ResponseEntity.failure("没有需要设置的角色资源信息");
        }
        String roleId = params.getString("roleId");
        JSONArray resArray = JSON.parseArray(params.getString("roleRes"));
        if (resArray.size() == 0) {
            return ResponseEntity.failure("没有需要设置的角色资源信息");
        }
        List<RoleRes> roleResList = new ArrayList<>();
        for (Object obj : resArray) {
            JSONObject resObj = JSON.parseObject(JSON.toJSONString(obj));
            RoleRes roleRes = new RoleRes();
            roleRes.setRoleId(roleId);
            roleRes.setResId(resObj.getString("id"));
            roleRes.setRemarks(resObj.getString("idKey"));
            roleResList.add(roleRes);
        }
        // 删除原有记录
        roleResDao.deleteResByRoleId(roleId);
        // 批量插入数据
        roleResDao.insertBatch(roleResList);
        return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
    }

    /**
     * 查询角色资源信息
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findRoleResTree(String roleId) throws Exception {
        List<RoleRes> roleResList = roleResDao.findByRoleId(roleId);
        if (roleResList!=null && roleResList.size()>0) {
            JSONArray resTree = new JSONArray();
            for (RoleRes roleRes : roleResList) {
                JSONObject resObj = new JSONObject();
                resObj.put("id", roleRes.getResId());
                resObj.put("idKey", roleRes.getRemarks());
                resObj.put("chkDisabled", false);
                resObj.put("checked", true);
                resTree.add(resObj);
            }
            return ResponseEntity.success(resTree);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 根据角色编号查询
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public List<RoleRes> findByRoleId(String roleId) throws Exception {
        return roleResDao.findByRoleId(roleId);
    }
}
